package com.design.pattern.singleton;

/**
 * 通过给代码块来加锁,实现单例
 */
public class LockBlockSingleton2 {
    public static int count = 0;

    /**
     * 构造方法私有化,防止直接new实例.
     */
    private LockBlockSingleton2() {
        count++;
    }

    /**
     * 持有本类
     */
    private static LockBlockSingleton2 instance;

    /**
     * 对外暴露的方法,返回单例实例(方法加锁)
     *
     * @return 单例实例
     */
    public static LockBlockSingleton2 getInstance() {
        if (null == instance) {
            synchronized (LockBlockSingleton2.class) {
                instance = new LockBlockSingleton2();
            }
        }
        return instance;
    }
}
